package cn.hy.dataSync.config;

import cn.hy.dataSync.constant.Delimiter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tangmengyang
 */

public enum DataBaseType {
    //
    MySql("mysql", "com.mysql.jdbc.Driver"),
    ;


    private String typeName;
    private String driverClass;

    DataBaseType(String typeName, String driverClass) {
        this.typeName = typeName;
        this.driverClass = driverClass;
    }

    public String getDriverClass() {
        return this.driverClass;
    }

    /**
     * 加上mysql url 其他信息
     * @param jdbcUrl jdbcUrl
     * @return jdbcUrl
     */
    public String appendJdbcSuffix(String jdbcUrl) {
        String result;
        String suffix;
        switch (this) {
            case MySql:
                suffix = "yearIsDateType=false&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&rewriteBatchedStatements=true&useSSL=false";
                if (jdbcUrl.contains(Delimiter.QUESTION_MARK)) {
                    result = jdbcUrl + Delimiter.AND + suffix;
                } else {
                    result = jdbcUrl + Delimiter.QUESTION_MARK + suffix;
                }
                break;
            default:
                throw new RuntimeException(String.format("unsupported database type: %s", getTypeName()));
        }

        return result;
    }

    public String formatPk(String splitPk) {
        String result = splitPk;

        switch (this) {
            case MySql:
                if (splitPk.length() >= 2 && splitPk.startsWith("`") && splitPk.endsWith("`")) {
                    result = splitPk.substring(1, splitPk.length() - 1).toLowerCase();
                }
                break;
            default:
                throw new RuntimeException(String.format("unsupported database type: %s", getTypeName()));
        }
        return result;
    }


    public String quoteColumnName(String columnName) {
        String result = columnName;

        switch (this) {
            case MySql:
                result = "`" + columnName.replace("`", "``") + "`";
                break;
            default:
                throw new RuntimeException(String.format("unsupported database type: %s", getTypeName()));
        }

        return result;
    }

    public String quoteTableName(String tableName) {
        String result = tableName;

        switch (this) {
            case MySql:
                result = "`" + tableName.replace("`", "``") + "`";
                break;
            default:
                throw new RuntimeException(String.format("unsupported database type: %s", getTypeName()));
        }

        return result;
    }

    private static final Pattern MYSQL_PATTERN = Pattern.compile("jdbc:mysql://(.+):\\d+/.+");

    public static DataBaseType parseDataBaseTypeFromJdbcUrl(String jdbcUrl) {
        Matcher mysql = MYSQL_PATTERN.matcher(jdbcUrl);
        if (mysql.matches()) {
            return MySql;
        }
        throw new RuntimeException(String.format("unsupported database type, jdbcUrl: %s", jdbcUrl));
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
}
